﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using FFACETools;

namespace Server
{
    static class Program
    {
        private static System.Windows.Forms.NotifyIcon _notifyIcon;
        private static System.Windows.Forms.ContextMenu _contextMenu;

        public static socketServer m_server = null;

        [STAThread]
        static void Main()
        {
            bool bVerified = true;
             
            Timer timm;
            System.IO.FileInfo fi = new System.IO.FileInfo("FFACE.dll");
            if (!fi.Exists)
            {
                MessageBox.Show("Server requires the FFACE library.\n\nYou may obtain this file at:\nhttp://FFEvo.com");
                bVerified = false;
            }
            if (bVerified)
            {
                timm = new Timer();
                timm.Interval = 1000;
                timm.Tick += new EventHandler(timm_Tick);
                _notifyIcon = new NotifyIcon();
                _notifyIcon.Text = "ChatterBox Server\n{0} Connections";//needs to be fixed.
                System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Server.icon.ico");
                _notifyIcon.Icon = new System.Drawing.Icon(stream);
                _notifyIcon.Visible = true;

                _contextMenu = new ContextMenu();
                _contextMenu.MenuItems.Add(new MenuItem("Exit", delegate { _notifyIcon.Dispose(); Application.Exit(); }));
                _notifyIcon.ContextMenu = _contextMenu;

                int iFFXiProcesses = System.Diagnostics.Process.GetProcessesByName("pol").Length;
                System.Diagnostics.Process process = null;

                // if we have more then 1 process
                if (1 < System.Diagnostics.Process.GetProcessesByName("pol").Length)
                {
                    // prompt for which process to attach too
                    MultipleProcesses mp = new MultipleProcesses();
                    //mp.MyParentForm = this;
                    if (mp.ShowDialog() == DialogResult.OK)
                        process = System.Diagnostics.Process.GetProcessById(mp.PID);
                }

                // if we only have 1 process, just attach to that one
                else if (1 == iFFXiProcesses)
                    process = System.Diagnostics.Process.GetProcessesByName("pol")[0];

                if (null == process)
                {
                    MessageBox.Show("No FFXi process found or selected");
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }

                m_server = new socketServer(process.Id);
                _notifyIcon.Text = string.Format(_notifyIcon.Text,m_server.m_Connections);//needs to be moved to the server so it can be updated.
                timm.Enabled = true;
                Application.Run();
            }
        }

        static void timm_Tick(object sender, EventArgs e)
        {//ugly hack that doesn't work needs to be removed.
            _notifyIcon.Text = string.Format(_notifyIcon.Text, m_server.m_Connections);
        }
    }
}
